<?php
App::uses('AppController', 'Controller');
/**
 * Groups Controller
 *
 * @property Group $Group
 * @property PaginatorComponent $Paginator
 */
class GroupsController extends AppController {

/**
 * Components
 *
 * @var array
 */
	var $controllerName = "Quản lý nhóm người dùng";
	public $components = array('Paginator');

	public function beforeFilter() {
        parent::beforeFilter();
         $this->Auth->allow('admin_add' );
    }
	public function index() {
		$this->Group->recursive = 0;

		$this->set('groups', $this->Paginator->paginate());
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Group->exists($id)) {
			throw new NotFoundException(__('Invalid group'));
		}
		$options = array('conditions' => array('Group.' . $this->Group->primaryKey => $id));
		$this->set('group', $this->Group->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		$this->set('saved', false);
		if ($this->request->is('post')) {
			$this->Group->create();
			if ($this->Group->save($this->request->data)) {
				$this->set('saved', true);
			} else {
				
			}
		}
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null,$slug = null) {
		//$this->Group->id = $this->params['id'];
		$this->set('saved', false);
		if (!$this->Group->exists($this->params['id'])) {
			throw new NotFoundException(__('Không tồn tại bản ghi này'));
		}
		if ($this->request->is(array('post', 'put'))) {
			$this->Group->id = $this->params['id']; 
			if ($this->Group->save($this->request->data)) {
				$this->set('saved', true);
			} else {
				//$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Group.' . $this->Group->primaryKey => $this->params['id']));
			$this->request->data = $this->Group->find('first', $options);
		}
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null,$slug = null) {
		$this->Group->id = $this->params['id'];
		if (!$this->Group->exists($this->params['id'])) {
			throw new NotFoundException(__('Không tồn tại bản ghi này'));
		}
		//$this->request->onlyAllow('post', 'delete');
		$check = $this->Group->User->find('first', array('conditions' => array('User.groups_id' => $this->Group->id)));
		if($check){
			$link = "http://".$_SERVER["HTTP_HOST"].$this->webroot."quan-ly-nhom-nhan-vien.html";
			echo '<script>';
			echo 'alert("Danh mục này chứa bản ghi con. Hãy xóa bản ghi con trước !");';
			$this->redirect(array('action' => 'index'));
			echo '</script>';	
			
		}else{
			$this->Group->delete();
			//$this->Session->setFlash(__('The group has been deleted.'));
			return $this->redirect(array('action' => 'index'));
			
		}
	}

/**
 * admin_index method
 *
 * @return void
 */
	public function admin_index() {
		$this->Group->recursive = 0;
		$this->set('groups', $this->Paginator->paginate());
	}

/**
 * admin_view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_view($id = null) {
		if (!$this->Group->exists($id)) {
			throw new NotFoundException(__('Invalid group'));
		}
		$options = array('conditions' => array('Group.' . $this->Group->primaryKey => $id));
		$this->set('group', $this->Group->find('first', $options));
	}

/**
 * admin_add method
 *
 * @return void
 */
	public function admin_add() {
		if ($this->request->is('post')) {
			$this->Group->create();
			if ($this->Group->save($this->request->data)) {
				$this->Session->setFlash(__('The group has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
			}
		}
	}

/**
 * admin_edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_edit($id = null) {
		if (!$this->Group->exists($id)) {
			throw new NotFoundException(__('Invalid group'));
		}
		if ($this->request->is(array('post', 'put'))) {
			if ($this->Group->save($this->request->data)) {
				$this->Session->setFlash(__('The group has been saved.'));
				return $this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
			}
		} else {
			$options = array('conditions' => array('Group.' . $this->Group->primaryKey => $id));
			$this->request->data = $this->Group->find('first', $options);
		}
	}

/**
 * admin_delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function admin_delete($id = null) {
		$this->Group->id = $id;
		if (!$this->Group->exists()) {
			throw new NotFoundException(__('Invalid group'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Group->delete()) {
			$this->Session->setFlash(__('The group has been deleted.'));
		} else {
			$this->Session->setFlash(__('The group could not be deleted. Please, try again.'));
		}
		return $this->redirect(array('action' => 'index'));
	}}
